package com.lc.hongtai.fragment;


import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lc.hongtai.MyApplication;
import com.lc.hongtai.R;
import com.lc.hongtai.adapter.Fourth_findAdapter;
import com.lc.hongtai.adapter.Fourth_findAdapter2;
import com.lc.hongtai.conn.JsonAccessToken;
import com.lc.hongtai.conn.JsonEconomics;
import com.lc.hongtai.conn.JsonMsglist;
import com.lc.hongtai.toast.LovelyToast;
import com.lc.hongtai.view.FullyLinearLayoutManager;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.helper.http.AsyCallBack;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import in.srain.cube.views.ptr.PtrClassicDefaultHeader;
import in.srain.cube.views.ptr.PtrFrameLayout;

/**
 * Created by Administrator on 2016/4/17.
 */
public class Fragment2 extends BaseFragment {

    String mTitle;
    private RecyclerView.Adapter mAdapter;
    private FullyLinearLayoutManager mLayoutManager;

    private RecyclerView rv01;
    private List<JsonMsglist.Info.ListContent> list = new ArrayList<>();
    private PtrFrameLayout store_house_ptr_frame;
    private PtrClassicDefaultHeader header;

    public int nowPage = 1;
    public int totalPages = 1;
    public int pageSize = 0;
    public int totalRows = 0;

    private boolean canLoadMore = true;


    Fourth_findAdapter2 mAdapter2;


    private List<JsonEconomics.Info.Article> articleList;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//        View view = inflater.inflate(R.layout.fr_simple_card, null);
        View view = BoundViewHelper.boundView(this, MyApplication.scaleScreenHelper.loadView((ViewGroup) inflater.inflate(R.layout.fragment2, null)));
        ButterKnife.bind(getActivity());

        rv01 = (RecyclerView) view.findViewById(R.id.rv_01);
        rv01.setLayoutManager(new LinearLayoutManager(getActivity()));

        //rv01.setAdapter(mAdapter = new Fourth_findAdapter(getActivity(),mDatas));
        articleList = new ArrayList<>();
        getAccessToken();

        //   rv01.setAdapter(mAdapter = new Fourth_findAdapter(getActivity(),mDatas));


        return view;
    }

    private void getAccessToken() {
        new JsonAccessToken("index/find", new AsyCallBack<JsonAccessToken.Info>() {
            @Override
            public void onSuccess(String toast, int type, JsonAccessToken.Info info) throws Exception {
                if (info != null) {
                    if (!TextUtils.isEmpty(info.access_token)) {
                        getData(info.access_token);
                    } else {
                        // UtilToast.show(getActivity(), "安全验证失败！");
                        LovelyToast.makeText(getActivity(), "安全验证失败！", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    }
                }
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                //  UtilToast.show(getActivity(), "安全验证失败！");
                LovelyToast.makeText(getActivity(), "安全验证失败！", 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);
            }
        }).execute(getActivity(), false);
    }

    public void getData(String access_token) {
        new JsonEconomics(access_token, 1, new AsyCallBack<JsonEconomics.Info>() {

            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);
                showLoading();
            }

            @Override
            public void onSuccess(String toast, int type, JsonEconomics.Info info) throws Exception {
                super.onSuccess(toast, type, info);
                articleList.addAll(info.article);

                rv01.setAdapter(mAdapter = new Fourth_findAdapter(getActivity(), articleList));

            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                dismissLoading();
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                dismissLoading();
            }
        }


        ).execute(getActivity(), false);


    }

}
